﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ToracTechnologies.ExpressionTreeLibrary.ExtensionMethods.Strings
{

    /// <summary>
    /// Extension Method Class To Run A Contains With A String Comparison
    /// </summary>
    public static class ContainsStringComparison
    {

        /// <summary>
        /// Extension Method For A String To See If It Contains A String And Let The User Use String Comparison
        /// </summary>
        /// <param name="StringToLookForValueIn">String Variable</param>
        /// <param name="ValueToCheckTheStringFor">Value to check inside the string</param>
        /// <param name="WhichComparison">Which comparison to use</param>
        /// <returns>Boolean if it contains that value</returns>
        /// <remarks>stringToCheckIn.Contains("ValueToCheckForInSide (stringToCheckIn)", StringComparison.OrdinalIgnoreCase);</remarks>
        public static bool Contains(this string StringToLookForValueIn, string ValueToCheckTheStringFor, StringComparison WhichComparison)
        {
            //Index Of -1 => Not Found
            //Index Of 0  => If str is empty or null

            //make sure the value we are checking for or the value we are checking against is not null
            if (string.IsNullOrEmpty(StringToLookForValueIn) || string.IsNullOrEmpty(ValueToCheckTheStringFor))
            {
                return false;
            }
            else
            {
                return StringToLookForValueIn.IndexOf(ValueToCheckTheStringFor, WhichComparison) >= 0;
            }
        }

        /// <summary>
        /// Extension Method For A String To See If It Contains A String And Let The User Use String Comparison
        /// </summary>
        /// <param name="StringsToLookThrough">String List To Look For The Value In</param>
        /// <param name="ValueToCheckTheStringFor">Value to check inside the string</param>
        /// <param name="WhichComparison">Which comparison to use</param>
        /// <returns>Boolean if it contains that value</returns>
        /// <remarks>stringToCheckIn.Contains("ValueToCheckForInSide (stringToCheckIn)", StringComparison.OrdinalIgnoreCase);</remarks>
        public static bool Contains(this IEnumerable<string> StringsToLookThrough, string ValueToCheckTheStringFor, StringComparison WhichComparison)
        {
            //Index Of -1 => Not Found
            //Index Of 0  => If str is empty or null

            //loop through all the strings and see if we can find a match
            foreach (string StringToTest in StringsToLookThrough)
            {
                //use the singlar method so we have code reuse
                if (StringToTest.Contains(ValueToCheckTheStringFor, WhichComparison))
                {
                    //we found a match, so return true
                    return true;
                }
            }

            //can't find the item
            return false;
        }


    }

}
